package net.jeeshop.web.action.front.h5.food;

import com.google.common.collect.Lists;
import net.jeeshop.core.dao.page.PagerModel;
import net.jeeshop.core.front.SystemManager;
import net.jeeshop.services.front.foodMenu.FoodMenuService;
import net.jeeshop.services.front.foodMenu.bean.FoodMenu;
import net.jeeshop.services.front.indexImg.IndexImgService;
import net.jeeshop.services.front.indexImg.bean.IndexImg;
import net.jeeshop.services.manage.foodCreateIndex.FoodCreateIndexService;
import net.jeeshop.services.manage.foodCreateIndex.bean.FoodCreateIndexMenu;
import net.jeeshop.web.action.front.FrontBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

/**
 * Created by xjli on 16/10/18.
 */

@Controller("h5FoodMenuController")
@RequestMapping("foodMenu")
public class FoodMenuController extends FrontBaseController<FoodMenu> {

    @Autowired
    private FoodMenuService foodMenuService;
    @Autowired
    private IndexImgService indexImgService;
    @Autowired
    private FoodCreateIndexService foodCreateIndexService;


    @RequestMapping(value = "getFoodMenus", method = RequestMethod.POST)
    @ResponseBody
    public PagerModel getFoodMenus(FoodMenu e, ModelMap model) throws IOException {

        PagerModel pagerModel = foodMenuService.selectPageList(e);
        if (pagerModel != null) {
            if (!CollectionUtils.isEmpty(pagerModel.getList())) {
                int i = 0;
                for (FoodMenu foodMenu : (List<FoodMenu>) pagerModel.getList()) {
                    if (!StringUtils.isEmpty(e.getRequestType()) && "home".equals(e.getRequestType())) {
                        if (i == 0) {
                            foodMenu.setExtra("今日推荐第一道菜");
                        }
                        if (i == 1) {
                            foodMenu.setExtra("今日推荐第二道菜");
                        }
                        if (i == 2) {
                            foodMenu.setExtra("今日推荐第三道菜");
                        }
                        i++;
                    }
                    if (!StringUtils.isEmpty(foodMenu.getMenuheadimage())) {
                        foodMenu.setMenuheadimage(SystemManager.getInstance().getSystemSetting().getImageRootPath() + foodMenu.getMenuheadimage());
                    }

                }
            }
        }
        return pagerModel;
    }


    @RequestMapping(value = "oneFoodMenu", method = RequestMethod.POST)
    @ResponseBody
    public FoodMenu oneFoodMenu(FoodMenu e, ModelMap model) throws IOException {

        FoodMenu foodMenu = foodMenuService.selectOne(e);
        if (foodMenu != null && !StringUtils.isEmpty(foodMenu.getMenuheadimage())) {
            foodMenu.setMenuheadimage(SystemManager.getInstance().getSystemSetting().getImageRootPath() + foodMenu.getMenuheadimage());
        }
        FoodCreateIndexMenu foodCreateIndexMenu = new FoodCreateIndexMenu();
        foodCreateIndexMenu.setFoodmenuid(Long.valueOf(foodMenu.getId()));
        List<FoodCreateIndexMenu> foodCreateIndexMenus = foodCreateIndexService.selectList(foodCreateIndexMenu);
        if (!CollectionUtils.isEmpty(foodCreateIndexMenus)) {
            for (FoodCreateIndexMenu foodCreateIndex : foodCreateIndexMenus) {
                if (!org.apache.commons.lang.StringUtils.isBlank(foodCreateIndex.getIndeximage())) {
                    foodCreateIndex.setIndeximage(SystemManager.getInstance().getSystemSetting().getImageRootPath() + foodCreateIndex.getIndeximage());

                }
            }
        }

        return foodMenu;
    }


    @RequestMapping(value = "getHomeBanners", method = RequestMethod.POST)
    @ResponseBody
    public List<IndexImg> getHomeBanners(IndexImg e, ModelMap model) throws IOException {


        List<IndexImg> indexImgs = Lists.newArrayList();
        e.setType("1");
        indexImgs = indexImgService.getHomeBanners(e);
        if (!CollectionUtils.isEmpty(indexImgs)) {
            for (IndexImg indexImg : indexImgs) {
                if (!StringUtils.isEmpty(indexImg.getPicture())) {
                    indexImg.setPicture(SystemManager.getInstance().getSystemSetting().getImageRootPath() + indexImg.getPicture());
                }
            }
        }

        return indexImgs;
    }


    @Override
    public FoodMenuService getService() {
        return foodMenuService;
    }
}
